﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Chk;

namespace ESOL.WinUI.Qc
{
    public partial class frmMaintainQcSampingParameters : BaseForm.frmBaseDialogyBusinessForm
    {
        #region Variable
        private QcSamplingParametersEntity mSelectedSamplingParameters;
        #endregion

        #region Constructor
        public frmMaintainQcSampingParameters()
        {
            InitializeComponent();
        }
        public frmMaintainQcSampingParameters(QcSamplingParametersEntity entity)
        {
            InitializeComponent();
            mSelectedSamplingParameters = entity;
        }
        #endregion

        #region Functions
        private void SetField()
        {
            if (mSelectedSamplingParameters != null)
            {
                txtName.Text = mSelectedSamplingParameters.Name;
                txtRangeMinQty.Text = mSelectedSamplingParameters.RangeMinQty.ToString();
                txtRangeMaxQty.Text = mSelectedSamplingParameters.RangeMaxQty.ToString();
                txtCheckQty.Text = mSelectedSamplingParameters.CheckQty.ToString();
                txtNgRate.Text = mSelectedSamplingParameters.NgRate.ToString();
            }
        }
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                MessageBox.Show("类型名称不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtRangeMinQty.Text))
            {
                MessageBox.Show("检验数量不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtRangeMinQty.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtRangeMaxQty.Text))
            {
                MessageBox.Show("检验数量不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtRangeMaxQty.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtCheckQty.Text))
            {
                MessageBox.Show("抽检数量不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtCheckQty.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtNgRate.Text))
            {
                MessageBox.Show("不良率不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtNgRate.Focus();
                return false;
            }
            if (Convert.ToDecimal(txtRangeMaxQty.Text) < Convert.ToDecimal(txtRangeMinQty.Text))
            {
                MessageBox.Show("抽检数量有误！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtRangeMaxQty.Focus();
                return false;
            }
            if (ThisActionType == ActionType.New)
            {
                string sql = @"select * from QC_SAMPLING_PARAMETERS
where NAME='" + txtName.Text + "'";
                DataTable dt = QcSamplingParametersBs.GetDtBySql(sql);
                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("类型名称已存在！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtName.Focus();
                    return false;
                }
            }
            if (ThisActionType == ActionType.Modify)
            {
                string sql = @"select * from QC_SAMPLING_PARAMETERS where NAME='" + txtName.Text + "' ";
                sql += " and ID!='" + mSelectedSamplingParameters.Id + "' ";
                DataTable dt = QcSamplingParametersBs.GetDtBySql(sql);
                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("类型名称已存在！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtName.Focus();
                    return false;
                }
            }
            return true;
        }
        private bool Save()
        {
            if (ThisActionType == ActionType.New)
            {
                mSelectedSamplingParameters = new QcSamplingParametersEntity();
                mSelectedSamplingParameters.Id = Guid.NewGuid().ToString();
                mSelectedSamplingParameters.Status = "STATUS01";
            }
            mSelectedSamplingParameters.Name = txtName.Text;
            mSelectedSamplingParameters.RangeMinQty = Convert.ToDecimal(txtRangeMinQty.Text);
            mSelectedSamplingParameters.RangeMaxQty = Convert.ToDecimal(txtRangeMaxQty.Text);
            mSelectedSamplingParameters.CheckQty = Convert.ToDecimal(txtCheckQty.Text);
            mSelectedSamplingParameters.NgRate = Convert.ToDecimal(txtNgRate.Text);
            if (ThisActionType == ActionType.New)
            {
                if (QcSamplingParametersBs.Save(true, mSelectedSamplingParameters))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if (ThisActionType == ActionType.Modify)
            {
                if (QcSamplingParametersBs.Save(false, mSelectedSamplingParameters))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }
        private void Init()
        {
            mSelectedSamplingParameters = null;
            txtName.Text = string.Empty;
            txtRangeMinQty.Text = string.Empty;
            txtRangeMaxQty.Text = string.Empty;
            txtCheckQty.Text = string.Empty;
            txtNgRate.Text = string.Empty;
        }

        #endregion

        #region Events
        private void frmQcBaseMaintain_Load(object sender, EventArgs e)
        {
            SetField();
        }
        protected override void Click_Save()
        {
            if (CheckData() == false)
                return;
            if (Save())
            {
                MessageBox.Show("保存成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("保存失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            //base.Click_Save();
        }
        protected override void Click_SaveNew()
        {
            if (CheckData() == false)
                return;
            if (Save())
            {
                MessageBox.Show("保存成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("保存失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            Init();
            ThisActionType = ActionType.New;
            //base.Click_SaveNew();
        }
        protected override void Click_Approve()
        {
            mSelectedSamplingParameters.Status = "STATUS03";
            if (QcSamplingParametersBs.UpdateQcSamplingParameters(mSelectedSamplingParameters))
            {
                MessageBox.Show("审核成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
            }
            //base.Click_Approve();
        }
        protected override void Click_Reject()
        {
            mSelectedSamplingParameters.Status = "STATUS04";
            if (QcSamplingParametersBs.UpdateQcSamplingParameters(mSelectedSamplingParameters))
            {
                MessageBox.Show("拒审成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
            }
            //base.Click_Reject();
        }
        #endregion
    }
}